<template>
  <el-dialog v-model="visible" :title="!dataForm.currentId ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter="onSubmit()">
      <el-form-item v-if="dataForm.type !== 2" label="分类图片" prop="pic">
        <!-- <pic-upload
          v-model="dataForm.pic"
          @update:model-value="checkPic"
        /> -->
        <el-upload action="#" :auto-upload="false" list-type="picture-card" :on-progress="handlePictureCardPreview"
          :on-change="handleUploadSuccess">
          <el-icon v-if="!dataForm.currentId">
            <Plus />
          </el-icon>
          <img v-if="dataForm.currentId" style="width: 136px;height: 134px;float: right;" alt="" :src="dataForm.pic"
              class="pic">
        </el-upload>
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="分类名称" prop="categoryName">
        <el-input v-model="dataForm.categoryName" controls-position="right" :min="0" label="分类名称" />
      </el-form-item>
      <el-form-item label="上级分类">
        <el-cascader v-model="selectedCategory" expand-trigger="hover" :options="categoryList"
          :props="categoryTreeProps" change-on-select :clearable="true" @change="handleChange" />
      </el-form-item>
      <el-form-item v-if="dataForm.type !== 2" label="排序号" prop="seq">
        <el-input-number v-model="dataForm.seq" controls-position="right" :min="0" label="排序号" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">
            下线
          </el-radio>
          <el-radio :label="1">
            正常
          </el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="visible = false">
          取消
        </el-button>
        <el-button type="primary" @click="onSubmit()">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ElMessage } from 'element-plus'
import { treeDataTranslate, idList } from '@/utils'
import { Debounce } from '@/utils/debounce'
const emit = defineEmits(['refreshDataList'])
const visible = ref(false)
const dataForm = reactive({
  categoryId: 0,
  currentId: 0,
  grade: 0,
  categoryName: '',
  seq: 1,
  status: 1,
  parentId: 0,
  pic: '',
  imgs: ''
})
const dataRule = reactive({
  categoryName: [
    { required: true, message: '分类名称不能为空', trigger: 'blur' },
    { pattern: /\s\S+|S+\s|\S/, message: '请输入正确的分类名称', trigger: 'blur' }
  ],
  imgs: [
    { required: true, message: '分类图片不能为空', trigger: 'blur' }
  ]
})
const categoryList = ref([])
const selectedCategory = ref([])
const categoryTreeProps = reactive({
  value: 'categoryId',
  label: 'categoryName',
  checkStrictly: true
})
const isSubmit = ref(false)
const dataFormRef = ref(null)
const init = (id) => {
  dataForm.currentId = id || 0
  dataForm.categoryId = id || 0
  http({
    url: http.adornUrl('/prod/category/listCategory'),
    method: 'get',
    params: http.adornParams()
  })
    .then(({ data }) => {
      categoryList.value = treeDataTranslate(data, 'categoryId', 'parentId')
    })
    .then(() => {
      visible.value = true
      nextTick(() => {
        dataFormRef.value?.resetFields()
        selectedCategory.value = []
      })
    })
    .then(() => {
      if (dataForm.categoryId) {
        // 修改
        http({
          url: http.adornUrl(`/prod/category/info/${dataForm.categoryId}`),
          method: 'get',
          params: http.adornParams()
        })
          .then(({ data }) => {
            dataForm.categoryId = data.categoryId
            dataForm.categoryName = data.categoryName
            dataForm.seq = data.seq
            dataForm.pic = data.pic
            dataForm.parentId = data.parentId
            dataForm.status = data.status
            selectedCategory.value = idList(categoryList.value, data.parentId, 'categoryId', 'children').reverse()
          })
      }
    })
}
defineExpose({ init })

const handleChange = (val) => {
  dataForm.parentId = val[val.length - 1]
}
// 表单提交
const onSubmit = Debounce(() => {
  if (selectedCategory.value.length === 1) {
    dataForm.grade = 0
  }
  if (selectedCategory.value.length === 2) {
    dataForm.grade = 1
  }
  if (selectedCategory.value.length === 3) {
    dataForm.grade = 2
  }
  dataFormRef.value?.validate((valid) => {
    if (valid) {
      if (isSubmit.value) {
        return
      }
      isSubmit.value = true
      const datas = {
        categoryId: dataForm.categoryId || '',
        categoryName: dataForm.categoryName,
        status: dataForm.status,
        seq: dataForm.seq,
        grade: dataForm.grade,
        parentId: dataForm.parentId,
        pic: dataForm.pic,
        imgs: dataForm.imgs
      }
      const formData = new FormData();
      for (const key in datas) {
        if (key !== 'imgs') {
          formData.append(key, datas[key]);
        } else {
          formData.append('files', datas.imgs);
        }
      }

      http({
        url: http.adornUrl(dataForm.categoryId == 0 ? '/prod/category/save' : '/prod/category/update'),
        method: dataForm.categoryId ? 'put' : 'post',
        headers: {
          'content-type': 'multipart/form-data;'
        },
        data: formData
      })
        .then(() => {
          ElMessage({
            message: '操作成功',
            type: 'success',
            duration: 1000,
            onClose: () => {
              isSubmit.value = false
              visible.value = false
              emit('refreshDataList')
            }
          })
        })
    }
  })
})

/**
 * 重新校验表单pic字段
 */
const checkPic = () => {
  dataFormRef.value?.validateField('pic')
}


const handlePictureCardPreview = (file) => {
  console.log(file, "00000000000");

  dataForm.imgs = file.raw
}
const handleUploadSuccess = (response, file, fileList) => {
  console.log(file, "00000000000");
  dataForm.imgs = file[0].raw
}
</script>
